/*
游戏场景背景界面，主要用来处理筹码的移动等效果(因为筹码要在桌子上且不能挡住其他控件的层级关系)
*/
var gameData = require("gameData");
var confige = require("confige");
cc.Class({
    extends: cc.Component,

    properties: {
        betItemPrefab:{
            default:null,
            type:cc.Node
        },

        betItemFrame0:{
            default:null,
            type:cc.SpriteFrame
        },
        betItemFrame1:{
            default:null,
            type:cc.SpriteFrame
        },
        betItemFrame2:{
            default:null,
            type:cc.SpriteFrame
        },
        betItemFrame3:{
            default:null,
            type:cc.SpriteFrame
        },
        betItemFrame4:{
            default:null,
            type:cc.SpriteFrame
        },
        sprArray :{
            default:[],
            type:[cc.SpriteFrame],
        },
    },

    onLoad: function () {
    },

    onInit:function(){
        gameData.gameBGNode = this;
        if(cc.sys.platform == cc.sys.MOBILE_BROWSER){
            this.bgNode = this.node.getChildByName("mainBg");
            this.bgNode.height = 790;
        }
        if(confige.roomData.roomType != "PaiGowBalcony")
        {
            this.node.getChildByName("mainBg").getComponent("cc.Sprite").spriteFrame = this.sprArray[window.localSetting.gameData.gameBgType-1]
        }

        this.mainBg = this.node.getChildByName("mainNode");
        this.scorePool = this.mainBg.getChildByName("scorePool");
        this.scorePoolLabel = this.scorePool.getChildByName("score").getComponent("cc.Label");
        this.scorePoolNum = 0;

        this.betItemListAll = {};
        this.betItemCount = 0;
        this.betBeginPosList = {};
        this.betFrameList = {};
        this.betFrameList[0] = this.betItemFrame0;
        this.betFrameList[1] = this.betItemFrame1;
        this.betFrameList[2] = this.betItemFrame2;
        this.betFrameList[3] = this.betItemFrame3;
        this.betFrameList[4] = this.betItemFrame4;

        this.sendBetDelayTime = 0.1;
    },

    initWithBankerMode:function(betNode){
        this.newBetNode = betNode;
        this.offSetY = -85;
        this.offSetX1 = [60,20];
        this.offSetX2 = [-20,-60];
        this.newBetOffsetList = [];
        for(var i=0;i<9;i++)
            this.newBetOffsetList[i] = [0,0];
    },

    bankerModeAddBet:function(chairB,chairE,betNum,isMe,isBanker){
        window.DKTools.audioEG.playEffect("tips_betItem");
        var newBetItem = cc.instantiate(this.betItemPrefab);
        var betIndex = 0;
        if(betNum == 2 || betNum == 5)
            betIndex = 1;
        if(betNum == 3 || betNum == 10)
            betIndex = 2;
        if(betNum == 4 || betNum == 15)
            betIndex = 3;
        if(betNum == 5 || betNum == 20)
            betIndex = 4;
        newBetItem.getComponent("cc.Sprite").spriteFrame = this.betFrameList[betIndex];
        var numLabel = newBetItem.getChildByName("num").getComponent("cc.Label");
        numLabel.string = betNum;
        this.newBetNode.addChild(newBetItem);
        newBetItem.x = this.betBeginPosList[chairB].x;
        newBetItem.y = this.betBeginPosList[chairB].y;

        this.betItemListAll[this.betItemCount] = newBetItem;
        this.betItemCount ++;
        var endPosx = this.betBeginPosList[chairE].x;
        var endPosy = this.betBeginPosList[chairE].y + this.offSetY;
        if(chairE <= 4){
            if(isBanker)
                endPosx += this.offSetX1[isMe];
            else
                endPosx += this.offSetX2[isMe];
        }
        else{
            if(isBanker)
                endPosx += this.offSetX2[isMe];
            else
                endPosx += this.offSetX1[isMe];
        }
        endPosy += this.newBetOffsetList[chairE][isMe];
        this.newBetOffsetList[chairE][isMe] += 3;

        var betMoveAction = cc.spawn(cc.moveTo(0.4, cc.p(endPosx,endPosy)),cc.scaleTo(0.4,0.4));
        betMoveAction.easing(cc.easeOut(2.0 + Math.random()*0.5));
        newBetItem.runAction(betMoveAction);
    },

    betItemListAddBetNew:function(chair,betNum){
        window.DKTools.audioEG.playEffect("tips_betItem");
        console.log("betItemListAddBet" + chair);
        if(this.betItemCount < 200)
        {
            var betIndex = 0;
            if(betNum == 2 || betNum == 5)
                betIndex = 1;
            if(betNum == 3 || betNum == 10)
                betIndex = 2;
            if(betNum == 4 || betNum == 15)
                betIndex = 3;
            if(betNum == 5 || betNum == 20)
                betIndex = 4;

            var newBetItem = cc.instantiate(this.betItemPrefab);
            newBetItem.getComponent("cc.Sprite").spriteFrame = this.betFrameList[betIndex];
            this.mainBg.addChild(newBetItem);
            newBetItem.x = this.betBeginPosList[chair].x;
            newBetItem.y = this.betBeginPosList[chair].y;

            this.betItemListAll[this.betItemCount] = newBetItem;
            this.betItemCount ++;
            var betMoveAction = cc.spawn(cc.moveTo(0.2, cc.p(0 - 100 + Math.random()*220, 0 - 50 + Math.random()*100)),
                                         cc.rotateBy(0.2, 1080 + Math.random()*200));
            betMoveAction.easing(cc.easeOut(2.0 + Math.random()*0.5));
            newBetItem.runAction(betMoveAction);
        }
    },

    betItemListAddBet:function(chair,betNum){
        window.DKTools.audioEG.playEffect("tips_betItem");
        console.log("betItemListAddBet" + chair);
        if(this.betItemCount < 200)
        {
            for(var i=0;i<betNum;i++)
            {
                var newBetItem = cc.instantiate(this.betItemPrefab);
                newBetItem.getComponent("cc.Sprite").spriteFrame = this.betFrameList[1];
                var numLabel = newBetItem.getChildByName("num").getComponent("cc.Label");
                numLabel.string = betNum;
                this.mainBg.addChild(newBetItem);
                newBetItem.x = this.betBeginPosList[chair].x;
                newBetItem.y = this.betBeginPosList[chair].y;

                this.betItemListAll[this.betItemCount] = newBetItem;
                this.betItemCount ++;
                var betMoveAction = cc.spawn(cc.moveTo(0.3, cc.p(0 - 100 + Math.random()*220, 0 - 50 + Math.random()*100)),
                                             cc.rotateBy(0.3, 1080 + Math.random()*200));
                betMoveAction.easing(cc.easeOut(2.0 + Math.random()*0.5));
                newBetItem.runAction(betMoveAction);
            }
        }
    },

    betItemListClean:function(){
        for(var i in this.betItemListAll)
        {
            this.betItemListAll[i].destroy();
        }
        this.betItemCount = 0;
        this.betItemListAll = {};
    },

    betItemRemove:function(chair,num){
        console.log("当前还剩下"+this.betItemCount+"个筹码！！!!!!");
        console.log("betItemRemove" + chair + "num===" + num);
        for(var i=0;i<num;i++)
        {
            this.betItemCount --;
            if(this.betItemCount < 0)
                continue;
            var curBetItem = this.betItemListAll[this.betItemCount];
            curBetItem.stopAllActions();

            var betMoveAction = cc.spawn(cc.moveTo(0.3, cc.p(this.betBeginPosList[chair].x, this.betBeginPosList[chair].y)),
                                         cc.rotateBy(0.3, 1080 + Math.random()*200));
            var betDestory = cc.callFunc(function () {
                // curBetItem.opacity = 0;
                curBetItem.destroy();
            }, this);
            curBetItem.runAction(cc.sequence(betMoveAction,cc.delayTime(0.1),betDestory));
        }
    },

    betItemRemove2:function(chair,num){
        console.log("当前还剩下"+this.betItemCount+"个筹码22222！！!!!!");
        console.log("22222betItemRemove" + chair + "num2222===" + num);
        for(var i=0;i<num;i++)
        {
            this.betItemCount --;
            if(this.betItemCount < 0)
                continue;
            var curBetItem = this.betItemListAll[this.betItemCount];
            curBetItem.stopAllActions();
            curBetItem.destroy();
        }
    },

    betItemCreate:function(betNum){
        console.log("betItemListAddBet" + betNum);
            for(var i=0;i<betNum;i++)
            {
                if(this.betItemCount < 200)
                {
                    var newBetItem = cc.instantiate(this.betItemPrefab);
                    newBetItem.getComponent("cc.Sprite").spriteFrame = this.betFrameList[3];
                    this.mainBg.addChild(newBetItem);
                    newBetItem.x = 0 - 100 + Math.random()*220;
                    newBetItem.y = 0 - 50 + Math.random()*100;

                    this.betItemListAll[this.betItemCount] = newBetItem;
                    this.betItemCount ++;
                }
            }
    },

    betItemCreate2:function(allNum){
        console.log("allNum ===== " + allNum);
        console.log("this.betItemCount ===== " + this.betItemCount);
        if(allNum > this.betItemCount)
        {
            var allCount = allNum - this.betItemCount;
            for(var i=0;i<allCount;i++)
            {
                if(this.betItemCount < 200)
                {
                    var newBetItem = cc.instantiate(this.betItemPrefab);
                    newBetItem.getComponent("cc.Sprite").spriteFrame = this.betFrameList[3];
                    this.mainBg.addChild(newBetItem);
                    newBetItem.x = 0 - 100 + Math.random()*220;
                    newBetItem.y = 0 - 50 + Math.random()*100;

                    this.betItemListAll[this.betItemCount] = newBetItem;
                    this.betItemCount ++;
                }
            }
        }else{
            var removeCount = this.betItemCount - allNum;
            for(var i=0;i<removeCount;i++)
            {
                this.betItemCount --;
                if(this.betItemCount < 0)
                    continue;
                var curBetItem = this.betItemListAll[this.betItemCount];
                curBetItem.stopAllActions();
                curBetItem.destroy();
                console.log("destroy11111");
            }
        }
        console.log("this.betItemCount2222222 ===== " + this.betItemCount);
    },

    betItemRemoveToBanker:function(bankerChair){
        window.DKTools.audioEG.playEffect("tips_betItem");
        console.log("betItemRemoveToBanker" + bankerChair);
        if(bankerChair == -1)
            bankerChair = 0;
        console.log(this.betItemListAll);
        console.log("this.betItemCount==="+this.betItemCount);
        for(var i=0;i<this.betItemCount;i++)
        {
            var curBetItem = this.betItemListAll[i];
            var betMoveAction = cc.spawn(cc.moveTo(0.2, cc.p(this.betBeginPosList[bankerChair].x, this.betBeginPosList[bankerChair].y)),
                                         cc.rotateBy(0.2, 900 + Math.random()*200));
            var betDestory = cc.callFunc(function () {
                curBetItem.opacity = 0;
                // curBetItem.destroy();
            }, this);
            curBetItem.runAction(cc.sequence(cc.delayTime(0.05*i),betMoveAction,cc.fadeOut(0.2)));
        }
    },

    betItemSendFromBanker:function(scoreList,bankerChair){
        console.log("betItemSendFromBanker");
        console.log(scoreList);
        var curBetCount = 0;
        for(var i in scoreList){
            if(confige.getCurChair(i) == bankerChair)
                continue;
            if(scoreList[i] > 0)
                curBetCount += 10//scoreList[i];
        }
        console.log("curBetCount = "+ curBetCount)
        for(var i=0;i<curBetCount;i++)
        {
            var newBetItem = cc.instantiate(this.betItemPrefab);
            newBetItem.getComponent("cc.Sprite").spriteFrame = this.betFrameList[0];//[Math.floor(Math.random()*100)%6];
            this.mainBg.addChild(newBetItem);
            newBetItem.x = this.betBeginPosList[bankerChair].x;
            newBetItem.y = this.betBeginPosList[bankerChair].y;
            this.betItemListAll[this.betItemCount] = newBetItem;
            this.betItemCount ++;
        }
        var self = this;
        for(var i in scoreList)
        {
            if(confige.getCurChair(i) == bankerChair)
                continue;
            if(scoreList[i] > 0)
            {
                for(var j=0;j<10;j++)
                {   
                    self.betItemCount --;
                    if(self.betItemCount < 0)
                        continue;
                    var sendBetOnceFunc = function(){
                        
                        var betMoveAction = cc.spawn(cc.moveTo(0.2, cc.p(self.betBeginPosList[confige.getCurChair(sendBetOnceFunc.i)].x, self.betBeginPosList[confige.getCurChair(sendBetOnceFunc.i)].y)),
                                                     cc.rotateBy(0.2, 1080 + Math.random()*200));
                        var betDestory = cc.callFunc(function () {
                            // sendBetOnceFunc.curBetItem.opacity = 0;
                            sendBetOnceFunc.curBetItem.destroy();
                        }, this);
                        if(confige.getCurChair(sendBetOnceFunc.i) == bankerChair)
                        {
                            sendBetOnceFunc.curBetItem.destroy();
                        }
                        else
                        {
                            window.DKTools.audioEG.playEffect("tips_betItem");
                            var moveActionPlus = cc.sequence(betMoveAction, cc.delayTime(0.1),betDestory);
                            moveActionPlus.easing(cc.easeOut(1.0)); 
                            sendBetOnceFunc.curBetItem.runAction(moveActionPlus);
                        }
                        
                    };
                    sendBetOnceFunc.curBetItem = self.betItemListAll[self.betItemCount];
                    sendBetOnceFunc.i = i;
                    this.scheduleOnce(sendBetOnceFunc, Math.random() * 0.7);//Math.floor(Math.random()*100));
                }
            }
        }
    },

    betItemSendFromBankerNew:function(scoreList,bankerChair){
        console.log("betItemSendFromBanker!!!!!!");
        console.log(scoreList);
        var self = this;
        var curChairCount = 0;
        for(var i in scoreList)
        {
            if(scoreList[i] > 0 && i != confige.getOriChair(bankerChair))
            {
                var curChairBet = window.DKTools.cutBetNum(parseInt(scoreList[i]));
                var curBetItemCount = 0;
                for(var j in curChairBet)
                {   
                    var newBetItem = cc.instantiate(this.betItemPrefab);
                    var betIndex = 0;
                    var betNum = curChairBet[j];
                    if(betNum == 2 || betNum == 5)
                        betIndex = 1;
                    if(betNum == 3 || betNum == 10)
                        betIndex = 2;
                    if(betNum == 4 || betNum == 15)
                        betIndex = 3;
                    if(betNum == 5 || betNum == 20)
                        betIndex = 4;
                    newBetItem.getComponent("cc.Sprite").spriteFrame = this.betFrameList[betIndex];//[Math.floor(Math.random()*100)%6];
                    this.mainBg.addChild(newBetItem);
                    newBetItem.x = this.betBeginPosList[bankerChair].x;
                    newBetItem.y = this.betBeginPosList[bankerChair].y;
                    this.betItemListAll[this.betItemCount] = newBetItem;
                    newBetItem.scale = 1;
                    this.betItemCount ++;
                    
                    var sendBetOnceFunc = function(){
                        window.DKTools.audioEG.playEffect("tips_betItem");
                        var betMoveAction = cc.spawn(cc.moveTo(0.5, cc.p(self.betBeginPosList[confige.getCurChair(sendBetOnceFunc.i)].x, self.betBeginPosList[confige.getCurChair(sendBetOnceFunc.i)].y)),
                                                     cc.rotateBy(0.5, 900 + Math.random()*200));
                        var betDestory = cc.callFunc(function () {
                            sendBetOnceFunc.curBetItem.opacity = 0;
                            // sendBetOnceFunc.curBetItem.destroy();
                        }, this);
                        var moveActionPlus = cc.sequence(cc.delayTime(sendBetOnceFunc.curBetItemCount*self.sendBetDelayTime),betMoveAction,cc.fadeOut(0.2));
                        moveActionPlus.easing(cc.easeOut(1.0));

                        sendBetOnceFunc.curBetItem.runAction(moveActionPlus);
                    };
                    sendBetOnceFunc.curBetItemCount = curBetItemCount;
                    sendBetOnceFunc.curBetItem = newBetItem;
                    sendBetOnceFunc.i = i;
                    this.scheduleOnce(sendBetOnceFunc, 0.15*curChairCount);//Math.floor(Math.random()*100));
                    curBetItemCount++;
                }
                curChairCount ++;
            }
        }
    },
});
